<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;

use App\Models\CompanyFolder;
use App\Models\Company;

class CompanyFolderController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(Request $request)
    {
        if(!$this->user->can('viewCompanyBox')) abort(405);
        
        /*搜索条件*/
        $query_arr = array_except($request->input(), ['_token']);
        $folder = CompanyFolder::where($query_arr)->paginate(20);
        foreach ($folder as $key => $val) {
            $folder[$key]['company'] = '测试企业2';
            $folder[$key]['job'] = '测试职位2';
            $folder[$key]['user_name'] = '测试用户2';
        }
        return view('admin.enterprise.folder.index')
                ->with('folder', $folder)
                ->with('companies', Company::all());
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy(Request $request)
    {
        if(!$this->user->can('deleteCompanyBox')) 
            return $this->formatJson('error',$this->permerror);
        
        $id = $request->input('id');
        $folder = CompanyFolder::find($id);
        if ($folder->delete())
            return  Response::json(array('status'=>'success','msg'=>'简历删除成功'));
        else
            return  Response::json(array('status'=>'error','msg'=>'删除失败'));
    }

    /**
     *
     *批量删除
     * @param  int  $id
     * @return Response
     */
    public function batchDelete(Request $request)
    {
        if(!$this->user->can('deleteCompanyBox'))
            return $this->formatJson('error',$this->permerror);
        
        $ids = explode(',', $request->input('id'));
        $folder = CompanyFolder::whereIn('id', $ids);
        $count = $folder->count();
        if ($folder->delete())
            return  Response::json(array('status'=>'success','msg'=>'成功删除 [' . $count . ']条 简历投递'));
        else
            return  Response::json(array('status'=>'error','msg'=>'删除失败'));
    }
}
